package game;

import game.maps.MapsCollection;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI extends JPanel {

	private static final long serialVersionUID = -7039197433461084699L;

	private static final JFrame frame = new JFrame();
	
	private static Model model;
	
	private static Movement movement;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		for (Sprite sprite : model.getSprites()) {
			g.setColor(sprite.getColor());
			g.fillRect(sprite.getX(), sprite.getY(), 
					sprite.getWidth(), sprite.getHeight());
		}
		
		for (Sprite sprite : model.getNetworkSprites()){
			g.setColor(sprite.getColor());
			g.fillRect(sprite.getX(), sprite.getY(), 
					sprite.getWidth(), sprite.getHeight());
		}
		
		drawInfoBar(g);

	}

	Color barColor = new Color(0.9f, 0.8f, 0.0f, 0.5f);
	Color textColor = new Color(0.2f, 0.1f, 0.2f);
	
	Font font = new Font(Font.SANS_SERIF,Font.BOLD,20);
	Font fontBig = new Font(Font.MONOSPACED,Font.BOLD,100);
	
	String bigText = null;
	long startedToShow;
	
	public void drawInfoBar(Graphics g){
		

		
		g.setColor(barColor);
		g.fillRect(10, 10, 680, 32);
		
		String p1 = model.getInfoBar().getPlayer1();
		char[] c1 = p1.toCharArray();
			
		g.setFont(font);
		g.setColor(textColor);
		g.drawChars(c1, 0, c1.length, 20, 33);
		
		String p2 = model.getInfoBar().getPlayer2();
		char[] c2 = p2.toCharArray();		
		g.drawChars(c2, 0, c2.length, 580, 33);
		
		g.setColor(Color.RED);
		
		String life1 = "Hit: " + model.getMyPlayer().getLife();
		char[] clife1 = life1.toCharArray();
		g.drawChars(clife1, 0, clife1.length, 150, 33);
		
		if (model.getMyPlayer().getLife()==20) {
			bigText = "YOU LOOSE";
			startedToShow = System.currentTimeMillis();
			model.getEnemyPlayer().setLife(0);
		}
		else if (model.getEnemyPlayer()!=null && model.getEnemyPlayer().getLife()==20){
			bigText = "YOU WIN";
			startedToShow = System.currentTimeMillis();
			model.getEnemyPlayer().setLife(0);
		}
		
		if (System.currentTimeMillis() - startedToShow < 3000) {
			char[] youwinChar = bigText.toCharArray();				
			g.setFont(fontBig);
			g.setColor(textColor);
			g.drawChars(youwinChar, 0, youwinChar.length, 100, 150);	
		}
			
		String life2;
		if (model.getEnemyPlayer()==null) life2 = "N/A";
		else {
			life2 = "Hit: " + model.getEnemyPlayer().getLife();		
		}
		char[] clife2 = life2.toCharArray();
		g.setFont(font);
		g.drawChars(clife2, 0, clife2.length, 500, 33);
	
	}

	public static void main(String[] args) {
		
		
		
		model = new Model();
		
		movement = new Movement(model);
		
		model.initialize(MapsCollection.getSaturdayMap(), movement);
		model.startNetwork();
		
		
		
		
		
		Audio audio = new Audio(model);
		audio.initialize();
		
		frame.setTitle("Catch&Go");
		frame.setSize(710, 640);


		frame.addKeyListener(new KeyboardControls(model, movement));

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				model.stopNetwork();
				System.exit(0);
			}
		});
		
		Container contentPane = frame.getContentPane();
		contentPane.add(new GUI());

		frame.setVisible(true);

		while (true) {
			
			movement.move();

			frame.repaint();

			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

}